<template>
  <!--大纲管理-->
  <div class="category">
    <ToolBar>
      <el-button type="primary" icon="el-icon-plus" size="small" @click="visible=true">添加</el-button>
    </ToolBar>
    <!--大纲本纲-->
    <el-tree :data="outlines">
      <div slot-scope="{data,node}" class="wrap" @mouseover="mouseover(data.id)" @mouseout="showOpt=0">
        <div class="title">
          <el-row type="flex">
            <el-col style="flex: 1">
              <span style="font-size: 13px">
                <span style="color: #3c8dbc">【 </span>{{data.outline_name}}
                <span style="color: #3c8dbc"> 】</span>
              </span>
            </el-col>
          </el-row>
        </div>
        <div class="buttonWrap" v-show="showOpt==data.id?true:false">
          <el-button type="mini" size="mini" circle icon="el-icon-caret-top" v-show="node.previousSibling" @click.stop="sort(data.id,'up')"></el-button>
          <el-button type="mini" size="mini" circle icon="el-icon-caret-bottom" v-show="node.nextSibling" @click.stop="sort(data.id,'down')"></el-button>
          <el-button type="primary" size="mini" @click.stop="edit(data)" style="backgroundColor:rgb(238, 250, 255);color:#3C8DBC">编辑</el-button>
          <el-button type="success" size="mini" @click.stop="add(data)" style="backgroundColor:rgba(103,194,58,.1);color:#67c23a;">添加子节点</el-button>
          <el-button type="danger" size="mini" @click.stop="del(data.id)" style="backgroundColor:rgb(255,255,255);color:#f56c6c">删除</el-button>
        </div>
      </div>
    </el-tree>

    <!--添加根节点-->
    <el-dialog :visible.sync='visible'>
      <div slot="title">
        <span style="color: #3c8dbc">- </span>添加根节点
        <span style="color: #3c8dbc"> -</span>
      </div>
      <el-form status-icon label-position="right" ref="form" :model="form" :rules="rules">
        <el-form-item label="名称" label-width="70px" prop="rootName">
          <el-input v-model="form.rootName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" size="mini" @click="rootCancel('form')">取消</el-button>
        <el-button type="primary" size="mini" @click="rootSave('form')">添加</el-button>
      </div>
    </el-dialog>

    <!--添加子节点-->
    <el-dialog :visible.sync='childVisible'>
      <div slot="title">
        <span style="color: #3c8dbc">- </span>添加子节点
        <span style="color: #3c8dbc"> -</span>
      </div>
      <el-form status-icon label-position="right" ref="form" :model="form" :rules="rules">
        <el-form-item label="名称" label-width="70px" prop="childName">
          <el-input v-model="form.childName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" size="mini" @click="childrenCancel('form')">取消</el-button>
        <el-button type="primary" size="mini" @click="childrenSave('form')">添加</el-button>
      </div>
    </el-dialog>

    <!--编辑-->
    <el-dialog :visible.sync='editVisible'>
      <div slot="title">
        <span style="color: #3c8dbc">- </span>编辑
        <span style="color: #3c8dbc"> -</span>
      </div>
      <el-form status-icon label-position="right" ref="form" :model="form" :rules="rules">
        <el-form-item label="名称" label-width="70px" prop="editName">
          <el-input v-model="form.editName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" size="mini" @click="editCancel('form')">取消</el-button>
        <el-button type="primary" size="mini" @click="editSave('form')">保存</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import ToolBar from '@/components/ToolBar.vue';
export default {
  components: {
    ToolBar
  },
  data() {
    return {
      outlines: [],
      visible: false,
      childVisible: false,
      editVisible: false,
      form: {
        rootName: '',
        childName: '',
        editName: '',
      },
      parentId: 0,
      editId: 0,
      loading: false,
      showOpt: 0,
      rules: {
        rootName: [
          { required: true, message: '请输入根大纲名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        childName: [
          { required: true, message: '请输入子大纲名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        editName: [
          { required: true, message: '请输入大纲名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getOutlines();
  },
  methods: {
    getOutlines() {
      this.loading = true;
      this.axios.get('/outlines').then((ret) => {
        let { data, error } = ret.data;
        this.outlines = data;
        this.loading = false;
      }).catch(() => { })
    },
    rootSave(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.axios.post('/outlines/add', {
            outline_name: this.form.rootName,
            parent_id: 0,
            status: 1
          }).then((ret) => {
            this.getOutlines();
            this.visible = false;
            this.form.rootName = '';
          }).catch(() => { })
        } else {
          this.$message.error('错了哦，请正确填写表单内容');
        }
      });
    },
    rootCancel(form) {
      this.$refs[form].resetFields();
      this.visible = false;
    },
    add(data) {
      this.childVisible = true;
      this.parentId = data.id;
    },
    edit(data) {
      this.editVisible = true;
      this.editId = data.id;
      this.form.editName = data.outline_name;
    },
    childrenSave(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.axios.post('/outlines/add', {
            outline_name: this.form.childName,
            parent_id: this.parentId,
            status: 1
          }).then((ret) => {
            this.getOutlines();
            this.childVisible = false;
            this.form.childName = '';
          }).catch(() => { })
        } else {
          this.$message.error('错了哦，请正确填写表单内容');
        }
      });
    },
    childrenCancel(form) {
      this.$refs[form].resetFields();
      this.childVisible = false;
    },
    editSave(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.axios.put('/outlines/edit/' + this.editId, {
            outline_name: this.form.editName
          }).then((ret) => {
            this.getOutlines();
            this.editVisible = false;
          }).catch(() => { })
        } else {
          this.$message.error('错了哦，请正确填写表单内容');
        }
      });
    },
    editCancel(form) {
      this.$refs[form].resetFields();
      this.editVisible = false;
    },
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.axios.delete('/outlines/del/' + id).then(() => {
          this.getOutlines();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch((ret) => {
          this.$message({
            type: 'warning',
            message: '请先删除子菜单'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    sort(id, dir) {
      this.axios.put('/outlines/sort/' + id + '/' + dir).then(() => {
        this.getOutlines();
      }).catch(() => { })
    },
    mouseover(id) {
      this.showOpt = id;
    }
  }
}
</script>
<style lang="less">
.category {
  .el-tree-node__content {
    height: 50px;
    line-height: 50px;
    .wrap {
      width: 100%;
      height: 50px;
      line-height: 50px;
      .title {
        float: left;
      }
      .buttonWrap {
        margin-right: 20px;
        float: right;
      }
    }
  }
}

.el-dialog__body {
  padding: 0 20px;
}
</style>
